//index.js
//获取应用实例
const app = getApp()

var demoData = require('../../utils/data.js');

var api = require('../../utils/api.js');

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    //magicList: demoData.magicList,
    magicList:[],
    magicPage:[],
    magicColorSchemList:[],
    isLoadAnimation:true,
    isReloadAction:false,
    bgColor:'white',
    loadingMagic:false,
    errorMessage:''
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  //页面完成加载
  onLoad: function () {

    var that = this;

    // 登录回调
    if (app.globalData.isLogin) {
      console.log('login callback 1');
      //this.loadFirstData();
    } else {
      app.onLogin = function () {
        //that.loadFirstData();
        console.log('login callback 2');
      }
      console.log('login callback 3');
    }


    //获取列表
    var errorMessage= '';
    var isReloadAction = false;
    api.getMagicList(
      {
        success: function (res) {

          if (res.list != false){
            //设置第一个data的bar颜色
            var firstBgColor = res.list[0]['colorScheme']['navBgColor'];
            var firstFrontColor = res.list[0]['colorScheme']['navFontColor'];
            console.log(firstFrontColor);
            wx.setNavigationBarColor({
              frontColor: firstFrontColor,
              backgroundColor: firstBgColor,
              animation: {
                duration: 400,
                timingFunc: 'easeIn'
              }
            })
          } else {
            errorMessage = '出问题啦， 没有数据啦';
            isReloadAction = true;
          }

          that.setData({
            magicList: res.list,
            magicPage: res.page,
            isLoadAnimation: false,
            errorMessage: errorMessage,
            isReloadAction: isReloadAction
          })

        },
        fail:function(res){
          that.setData({
            isLoadAnimation: false,
            isReloadAction:true,
            errorMessage:'网络通信出错拉。。。'
          })
        },
        pageNumber:1,
        userId:2
      },
    )

    if (this.data.magicList.indexOf[0]){
      
    }

  },
  setLoading: function (e) {
    this.setData({
      loading: !this.data.loading
    })
  },
  
  changeSubject:function(event){

    var that = this;

    // 当前位置
    var current = event.detail.current;
    
    //var magicInfo = this.data.magicList[current];
  
    //如果滑动到最后，需要加载新的数据
    if ( (current+1) %10 == 0){
      if (this.data.magicPage.pageSize != '') {
        if (this.data.magicPage.pageNumber < this.data.magicPage.pageTotalNumber){

          var nextPageNumber = this.data.magicPage.pageNumber + 1;

          //追加列表
          api.getMagicList(
            {
              success: function (res) {
                that.setData({
                  magicList: that.data.magicList.concat(res.list),
                  magicPage: res.page
                })
              },
              pageNumber: nextPageNumber,
              userId: 2
            },
          )
        }
      }
      //console.log('load more data');
    }

    
    /*设置bar上的颜色*/
    wx.setNavigationBarColor({
      frontColor: this.data.magicList[current]['colorScheme']['navFontColor'],
      backgroundColor: this.data.magicList[current]['colorScheme']['navBgColor'],
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })

  },

  //
  questionMagic:function(event){

    var that = this;

    var dataset = event.currentTarget.dataset;
    var indexId = dataset.indexId; 
    var magicId = dataset.magicId; 

    //console.log(dataset);

    var changeMagicList = this.data.magicList;
    
    that.setData({
      loadingMagic:true
    })

    //changeMagicList[indexId]['status'] = 1;
    //changeMagicList[indexId]['answer'] = changeMagicList[indexId]['content'];

    api.questionMagic(
      {
        success:function(res){
          changeMagicList[indexId]['magicContent'] = res.magicContent;
          that.setData({
            magicList: changeMagicList,
            loadingMagic: false
          })
        }
      },
      magicId
    )

  },

  //重新加载
  reload:function(){
    var that = this;

    that.setData({
      isLoad: true,
      isReloadAction:false,
      errorMessage:''
    })

    firstLoad(that);

  },

  bindanimationfinish:function(event){
    var that = this;
    // 当前位置
    var current = event.detail.current;
    if (current == 0){
      console.log('bindanimationfinish');
      refresh(that, 2);
    }


  },

  goMagicPage:function(event){
    wx.redirectTo({
      url: '/pages/magic/index'
    })
  },
  goDivinationPage: function (event) {
    wx.redirectTo({
      url: '/pages/divination/index'
    })
  }
  
})

function refresh(that, userId){
  api.getMagicList(
    {
      success: function (res) {

        if (res.list != false) {
          //设置第一个data的bar颜色
          var firstBgColor = res.list[0]['colorScheme']['navBgColor'];
          var firstFrontColor = res.list[0]['colorScheme']['navFontColor'];

          wx.setNavigationBarColor({
            frontColor: firstFrontColor,
            backgroundColor: firstBgColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          })

          that.setData({
            magicList: res.list,
            magicPage: res.page,
          })
        }

      },
      pageNumber: 1,
      userId: 2
    },
  )

}

function firstLoad(that){

  var errorMessage = '';
  var isReloadAction = false;

  api.getMagicList(
    {
      success: function (res) {

        if (res.list != false) {
          //设置第一个data的bar颜色
          var firstBgColor = res.list[0]['colorScheme']['navBgColor'];
          var firstFrontColor = res.list[0]['colorScheme']['navFontColor'];

          wx.setNavigationBarColor({
            frontColor: firstFrontColor,
            backgroundColor: firstBgColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          })
        } else {
          errorMessage = '出问题啦， 没有数据啦';
          isReloadAction = true;
        }

        that.setData({
          magicList: res.list,
          magicPage: res.page,
          isLoadAnimation: false,
          errorMessage: errorMessage,
          isReloadAction: isReloadAction
        })

      },
      fail: function (res) {
        that.setData({
          isLoadAnimation: false,
          isReloadAction: true,
          errorMessage: '网络通信出错拉。。。'
        })
      },
      pageNumber: 1,
      userId: 2
    },
  )

}
